package com.mmall.product.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mmall.common.base.BooleanResult;
import com.mmall.common.enums.BooleanEnum;
import com.mmall.common.enums.ProductStatusEnum;
import com.mmall.common.util.BeanUtil;
import com.mmall.common.base.PageResult;
import com.mmall.product.mapper.ProductBlobMapper;
import com.mmall.product.mapper.ProductMapper;
import com.mmall.pojo.entity.ProductBlobEntity;
import com.mmall.pojo.entity.ProductEntity;
import com.mmall.pojo.param.AddProductParam;
import com.mmall.pojo.param.ProductPageParam;
import com.mmall.pojo.param.UpdateProductParam;
import com.mmall.pojo.vo.ProductVo;
import com.mmall.product.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 商品服务实现类
 *
 * @author: gg
 * @date: 2018/12/10 17:52
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductBlobMapper productBlobMapper;

    /**
     * 添加商品
     *
     * @param addProductParam AddProductParam
     * @return ProductVo
     */
    @Override
    public ProductVo add(AddProductParam addProductParam) {
        ProductEntity productEntity = BeanUtil.map(addProductParam, ProductEntity.class);
        //如果已经存在了该分类 品牌 名称的商品，则报错
        ProductEntity productEntityFromDb = productMapper.selectByCategoryIdAndBrandIdAndName(productEntity);
        if (productEntityFromDb != null) {
            throw new IllegalArgumentException("已经存在该商品");
        }
        productEntity.setIsDel(BooleanEnum.FALSE.getValue());
        productEntity.setStatus(ProductStatusEnum.WAIT_FOR.getCode());
        productMapper.insertSelective(productEntity);

        String detail = addProductParam.getDetail();
        String mainImg = addProductParam.getMainImage();
        String subImages = addProductParam.getSubImages();

        ProductBlobEntity productBlobEntity = new ProductBlobEntity();
        productBlobEntity.setId(productEntity.getId());
        productBlobEntity.setDetail(detail);
        productBlobEntity.setMainImage(mainImg);
        productBlobEntity.setIsDel(BooleanEnum.FALSE.getValue());
        productBlobEntity.setSubImages(subImages);
        //这里是sql优化，varchar类型大字段单独建一张表
        productBlobMapper.insertSelective(productBlobEntity);
        return getProductVo(productEntity, productBlobEntity);
    }

    /**
     * 编辑商品
     *
     * @param updateProductParam UpdateProductParam
     * @return ProductVo
     */
    @Override
    public ProductVo update(UpdateProductParam updateProductParam) {
        ProductEntity productEntity = BeanUtil.map(updateProductParam, ProductEntity.class);
        Long id = productEntity.getId();
        //如果已经存在了该分类 品牌 名称的商品，则报错
        ProductEntity productEntityFromDb = productMapper.selectByCategoryIdAndBrandIdAndName(productEntity);
        if (productEntityFromDb != null && !productEntityFromDb.getId().equals(id)) {
            throw new IllegalArgumentException("已经存在该商品");
        }
        productMapper.updateByPrimaryKeySelective(productEntity);

        String detail = updateProductParam.getDetail();
        String mainImg = updateProductParam.getMainImage();
        String subImages = updateProductParam.getSubImages();

        ProductBlobEntity productBlobEntity = new ProductBlobEntity();
        productBlobEntity.setId(id);
        productBlobEntity.setDetail(detail);
        productBlobEntity.setMainImage(mainImg);
        productBlobEntity.setSubImages(subImages);
        //这里是sql优化，varchar类型大字段单独建一张表
        productBlobMapper.updateByPrimaryKeySelective(productBlobEntity);
        return getProductVo(productEntity, productBlobEntity);
    }


    /**
     * 分页查询
     *
     * @param productPageParam ProductPageParam
     * @return PageResult
     */
    @Override
    public PageResult<ProductVo> page(ProductPageParam productPageParam) {
        PageResult<ProductVo> pageResult = new PageResult<>();
        long count = productMapper.selectCount(productPageParam);
        pageResult.setTotal(count);
        if (count > 0) {
            List<ProductEntity> productEntityList = productMapper.selectPage(productPageParam);
            List<Long> idList = Lists.newArrayList();
            Map<Long, ProductEntity> map = Maps.newHashMap();
            productEntityList.forEach(productEntity -> {
                idList.add(productEntity.getId());
                map.put(productEntity.getId(), productEntity);
            });
            List<ProductBlobEntity> productBlobEntityList = productBlobMapper.selectByIdList(idList);
            List<ProductVo> productVoList = Lists.newArrayList();
            productBlobEntityList.forEach(productBlobEntity -> {
                ProductEntity productEntity = map.get(productBlobEntity.getId());
                ProductVo productVo = getProductVo(productEntity, productBlobEntity);
                productVoList.add(productVo);
            });
            pageResult.setList(productVoList);
        }
        return pageResult;
    }

    /**
     * 商品详情
     *
     * @param id 商品id
     * @return ProductVo
     */
    @Override
    public ProductVo detail(Long id) {
        ProductEntity productEntity = productMapper.selectByPrimaryKey(id);
        ProductBlobEntity productBlobEntity = productBlobMapper.selectByPrimaryKey(id);
        return getProductVo(productEntity, productBlobEntity);
    }

    /**
     * 删除
     *
     * @param id 商品id
     * @return BooleanResult
     */
    @Override
    public BooleanResult del(Long id) {
        productMapper.deleteByPrimaryKey(id);
        productBlobMapper.deleteByPrimaryKey(id);
        return BooleanResult.success();
    }


    private ProductVo getProductVo(ProductEntity productEntity, ProductBlobEntity productBlobEntity) {
        ProductVo productVo = BeanUtil.map(productEntity, ProductVo.class);
        productVo.setDetail(productBlobEntity.getDetail());
        productVo.setMainImage(productBlobEntity.getMainImage());
        productVo.setSubImages(productBlobEntity.getSubImages());
        return productVo;
    }


}
